﻿package com.farm.code.web.action;

import com.farm.code.domain.FarmCodeGuide;
import com.farm.code.domain.FarmCodeProject;
import com.farm.code.domain.FarmCodeType;
import com.farm.code.server.FarmCodeGuideManagerInter;
import com.farm.code.server.FarmCodeProjectManagerInter;
import com.farm.code.server.FarmCodeTypeManagerInter;

import com.farm.web.WebSupport;
import com.farm.web.easyui.EasyUiTreeNode;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import org.apache.struts2.json.JSONUtil;

import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.spring.BeanFactory;

/**
 * 代码工程运行向导
 * 
 * @author autoCode
 * 
 */
public class ActionFarmCodeGuideQuery extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private FarmCodeGuide entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String id;// 异步树传入的id
	private List<EasyUiTreeNode> treeNodes;// 异步树的封装
	private FarmCodeProject project;
	private FarmCodeType protype;
	private List<String> tables;
	private List<Map<String, Object>> datas;
	private String dataStr;
	
	private String workpath;

	public String loadTreeNode() {
		if (id == null) {
			id = "NONE";
		}
		// 查询当前节点
		DataQuery query1 = DataQuery
				.getInstance(
						"1",
						"ID,NAME,PARENTID,name as CTIME",
						"((SELECT id,NAME,parentid FROM  farm_code_type WHERE parentid='"
								+ id
								+ "') UNION (SELECT id,NAME,TYPEID AS parentid FROM  farm_code_project WHERE TYPEID='"
								+ id + "'  AND pstate='1')) a");
		query1.setPagesize(100);
		// 查询子节点
		DataQuery query2 = DataQuery
				.getInstance(
						"1",
						"b.id as ID,b.NAME as NAME,b.parentid as PARENTID,b.NAME as CTIME",
						"((SELECT id,NAME,parentid FROM  farm_code_type WHERE parentid='"
								+ id
								+ "') UNION (SELECT id,NAME,TYPEID AS parentid FROM  farm_code_project WHERE TYPEID='"
								+ id
								+ "')) a LEFT JOIN  ((SELECT id,NAME,parentid FROM  farm_code_type ) UNION (SELECT id,NAME,TYPEID AS parentid FROM  farm_code_project where PSTATE ='1'  AND pstate='1' )) b  ON b.parentid=a.id ");
		query2.setPagesize(100);
		query2.addSqlRule(" and b.id IS NOT NULL");
		try {
			treeNodes = EasyUiTreeNode.formatAsyncAjaxTree(query1.search()
					.getResultList(), query2.search().getResultList(),
					"PARENTID", "ID", "NAME", "CTIME");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 加载并显示模板参数
	 * 
	 * @return
	 */
	public String loadParas() {
		try {
			jsonResult = aloneIMP.getTemplateParas(ids);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			DataResult result = aloneIMP.createFarmCodeGuideSimpleQuery(query)
					.search();
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	public String jdbcQueryall() {
		try {
			DataResult result = aloneIMP.getDatabaseField(aloneIMP
					.getFarmCodeGuideEntity(proIMP
							.getFarmCodeProjectEntity(ids).getGuideid()));
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 生成代码
	 * 
	 * @return
	 */
	public String runTemplate() {
		try {
			aloneIMP.runTemplate(ids,workpath);
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交字段数据
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String gridSubmit() {
		try {
			List<Map<String, Object>> list = (List<Map<String, Object>>) JSONUtil
					.deserialize(dataStr);
			// 保存表字段并生成map
			aloneIMP.editFarmCodeGuideTableField(ids, list);
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = aloneIMP.insertFarmCodeGuideEntity(entity,
					getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交并链接数据库
	 * 
	 * @return
	 */
	public String linkSubmit() {
		try {
			// 链接数据库
			tables = aloneIMP.getDatabaseTables(entity);
			// 保存数据
			entity = aloneIMP.editProjectJDBCSetings(ids, entity,
					getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	public String addAppSetingsSubmit() {
		try {
			// 保存数据
			entity = aloneIMP.editProjectAppSetings(ids, entity,
					getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.deleteFarmCodeGuideEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		try {
			project = proIMP.getFarmCodeProjectEntity(ids);
			if (project == null) {
				protype = typeIMP.getFarmCodeTypeEntity(ids);
				return "protype";
			} else {
				entity = aloneIMP.getFarmCodeGuideEntity(project.getGuideid());
				if (project.getType().equals("0")) {
					return "jdbc";
				} else {
					return "func";
				}
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static FarmCodeGuideManagerInter aloneIMP = (FarmCodeGuideManagerInter) BeanFactory
			.getBean("farm_code_guideProxyId");
	private final static FarmCodeProjectManagerInter proIMP = (FarmCodeProjectManagerInter) BeanFactory
			.getBean("farm_code_projectProxyId");
	private final static FarmCodeTypeManagerInter typeIMP = (FarmCodeTypeManagerInter) BeanFactory
			.getBean("farm_code_typeProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public FarmCodeGuide getEntity() {
		return entity;
	}

	public void setEntity(FarmCodeGuide entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}
	
	public String getWorkpath() {
		return workpath;
	}

	public void setWorkpath(String workpath) {
		this.workpath = workpath;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDataStr() {
		return dataStr;
	}

	public void setDataStr(String dataStr) {
		this.dataStr = dataStr;
	}

	public List<EasyUiTreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<EasyUiTreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	public FarmCodeProject getProject() {
		return project;
	}

	public void setProject(FarmCodeProject project) {
		this.project = project;
	}

	public FarmCodeType getProtype() {
		return protype;
	}

	public void setProtype(FarmCodeType protype) {
		this.protype = protype;
	}

	public List<String> getTables() {
		return tables;
	}

	public void setTables(List<String> tables) {
		this.tables = tables;
	}

	public List<Map<String, Object>> getDatas() {
		return datas;
	}

	public void setDatas(List<Map<String, Object>> datas) {
		this.datas = datas;
	}

//	private static final Logger log = Logger
//			.getLogger(ActionFarmCodeGuideQuery.class);
	private static final long serialVersionUID = 1L;
	/**
	 * 加载树节点 public String loadTreeNode() { treeNodes =
	 * EasyUiTreeNode.formatAjaxTree(EasyUiTreeNode .queryTreeNodeOne(id,
	 * "SORT", "alone_menu", "ID", "PARENTID", "NAME").getResultList(),
	 * EasyUiTreeNode .queryTreeNodeTow(id, "SORT", "alone_menu", "ID",
	 * "PARENTID", "NAME").getResultList(), "PARENTID", "ID", "NAME"); return
	 * SUCCESS; }
	 * 
	 * @return
	 */
}
